package com.fanxl.securityfive.config;

import com.fanxl.securityfive.bean.MyUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Database {

    private Map<String, MyUser> data;

    private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    public Database() {
        data = new HashMap<>();

        MyUser jack = new MyUser(1, "jack", getPassword("jack123"), getGrants("ROLE_USER"));
        MyUser danny = new MyUser(2, "danny", getPassword("danny123"), getGrants("ROLE_EDITOR"));
        MyUser alice = new MyUser(3, "alice", getPassword("alice123"), getGrants("ROLE_REVIEWER"));
        MyUser smith = new MyUser(4, "smith", getPassword("smith123"), getGrants("ROLE_ADMIN"));
        data.put("jack", jack);
        data.put("danny", danny);
        data.put("alice", alice);
        data.put("smith", smith);
    }

    public Map<String, MyUser> getDatabase() {
        return data;
    }

    private String getPassword(String raw) {
        return passwordEncoder.encode(raw);
    }

    private Collection<GrantedAuthority> getGrants(String role) {
        return AuthorityUtils.commaSeparatedStringToAuthorityList(role);
    }
}